package com.anolesoft.twelvePlan.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.twelvePlan.entity.MesEqualCatalogDO;
import com.anolesoft.twelvePlan.entity.MesGyInfoDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @author zyq
 * @date 2022/3/21
 */
@Mapper
public interface MesEqualCatalogMapper extends BaseMapper<MesEqualCatalogDO> {
    @SelectProvider(type = createSQL.class, method = "querySQL")
    List<MesEqualCatalogDO> queryEqualCatalog(@Param("mesEqualCatalogDO") MesEqualCatalogDO mesEqualCatalogDO, Page<MesEqualCatalogDO> page);
    public static class createSQL {
        public static String querySQL(@Param("mesEqualCatalogDO") MesEqualCatalogDO mesEqualCatalogDO, Page<MesEqualCatalogDO> page) {
            return new SQL() {
                {
                    StringBuffer sql = new StringBuffer();
                    sql.append(" * from mes_equal_catalog a where a.deleteflag = 0 ");
                    sql.append(" and a.factory_id = #{mesEqualCatalogDO.factoryId, jdbcType=VARCHAR}");
                    if (!StrUtil.isEmptyOrUndefined(mesEqualCatalogDO.getJx())) {
                        sql.append(" and a.jx = trim(upper(#{mesEqualCatalogDO.jx, jdbcType=VARCHAR}))");
                    }
                    if (!StrUtil.isEmptyOrUndefined(mesEqualCatalogDO.getPartid())) {
                        sql.append(" and a.partid = trim(upper(#{mesEqualCatalogDO.partid, jdbcType=VARCHAR}))");
                    }
                    if (!StrUtil.isEmptyOrUndefined(mesEqualCatalogDO.getFlag())) {
                        sql.append(" and a.flag = trim(#{mesEqualCatalogDO.flag, jdbcType=VARCHAR})");
                    }
                    SELECT(sql.toString());
                    System.out.println("queryEqualCatalogSQL:" + sql.toString());
                }
            }.toString();
        }
    }

    @SelectProvider(type = createSQL2.class, method = "repeatEqualCatalogSQL")
    int repeatEqualCatalog(@Param("mesEqualCatalogDO") MesEqualCatalogDO mesEqualCatalogDO);
    public static class createSQL2 {
        public static String repeatEqualCatalogSQL(@Param("mesEqualCatalogDO") MesEqualCatalogDO mesEqualCatalogDO){
            return new SQL(){
                {
                    StringBuffer sql = new StringBuffer();
                    sql.append(" count(*) from mes_equal_catalog a where a.jx = trim(upper(#{mesEqualCatalogDO.jx, jdbcType=VARCHAR})) ");
                    sql.append(" and a.partid = trim(upper(#{mesEqualCatalogDO.partid, jdbcType=VARCHAR})) ");
                    sql.append(" and a.deleteflag = 0");
                    if (!StrUtil.isEmptyOrUndefined(mesEqualCatalogDO.getObjuid())){
                        sql.append(" and a.objuid != #{mesEqualCatalogDO.objuid, jdbcType=VARCHAR}");
                    }
                    SELECT(sql.toString());
                    System.out.println(">>>>repeatEqualCatalogSQL:" + sql.toString());
                }
            }.toString();
        }
    }

    @Insert("insert into mes_equal_catalog (jx, partid, flag, createuid, createtime, factory_id) " +
            "values (trim(upper(#{mesEqualCatalogDO.jx, jdbcType=VARCHAR})), trim(upper(#{mesEqualCatalogDO.partid, jdbcType=VARCHAR})), " +
            " #{mesEqualCatalogDO.flag, jdbcType=VARCHAR}, #{mesEqualCatalogDO.createtime, jdbcType=VARCHAR}, " +
            " sysdate, #{mesEqualCatalogDO.factoryId, jdbcType=VARCHAR})")
    int insertEqualCatalog(@Param("mesEqualCatalogDO") MesEqualCatalogDO mesEqualCatalogDO);

    @Update("update mes_equal_catalog a set " +
            " a.jx = trim(upper(#{mesEqualCatalogDO.jx, jdbcType=VARCHAR}))," +
            " a.partid = trim(upper(#{mesEqualCatalogDO.partid, jdbcType=VARCHAR}))," +
            " a.flag = #{mesEqualCatalogDO.flag, jdbcType=VARCHAR}," +
            " a.modifyuid = #{mesEqualCatalogDO.modifyuid, jdbcType=VARCHAR}," +
            " a.modifytime = sysdate " +
            " where a.objuid = #{mesEqualCatalogDO.objuid, jdbcType=VARCHAR}")
    int updateEqualCatalog(@Param("mesEqualCatalogDO") MesEqualCatalogDO mesEqualCatalogDO);

    @Update("update mes_equal_catalog a set " +
            " a.deleteflag = 1, " +
            " a.deleteuid = #{mesEqualCatalogDO.deleteuid, jdbcType=VARCHAR}, " +
            " a.deletetime = sysdate " +
            " where a.objuid = #{mesEqualCatalogDO.objuid, jdbcType=VARCHAR}")
    int deleteEqualCatalog(@Param("mesEqualCatalogDO") MesEqualCatalogDO mesEqualCatalogDO);

    @SelectProvider(type = createSQL3.class, method = "exportSQL")
    int exportEqualCatalog(@Param("mesEqualCatalogDO") MesEqualCatalogDO mesEqualCatalogDO);
    public static class createSQL3 {
        public static String exportSQL(@Param("mesEqualCatalogDO") MesEqualCatalogDO mesEqualCatalogDO) {
            return new SQL() {
                {
                    StringBuffer sql = new StringBuffer();
                    sql.append(" count(*) from mes_equal_catalog a where a.deleteflag = 0 ");
                    sql.append(" and a.factory_id = #{mesEqualCatalogDO.factoryId, jdbcType=VARCHAR}");
                    if (!StrUtil.isEmptyOrUndefined(mesEqualCatalogDO.getJx())) {
                        sql.append(" and a.jx = trim(upper(#{mesEqualCatalogDO.jx, jdbcType=VARCHAR}))");
                    }
                    if (!StrUtil.isEmptyOrUndefined(mesEqualCatalogDO.getPartid())) {
                        sql.append(" and a.partid = trim(upper(#{mesEqualCatalogDO.partid, jdbcType=VARCHAR}))");
                    }
                    if (!StrUtil.isEmptyOrUndefined(mesEqualCatalogDO.getFlag())) {
                        sql.append(" and a.flag = trim(#{mesEqualCatalogDO.flag, jdbcType=VARCHAR}))");
                    }
                    SELECT(sql.toString());
                    System.out.println("exportSQL:" + sql.toString());
                }
            }.toString();
        }
    }
}
